package cn.acewill.yxd.yunxundian.ui.user;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import cn.acewill.yxd.yunxundian.BaseActivity;

/**
 * Created by XXX on 2017/8/3.
 */

public class MyNotifictionService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //定时任务
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        //启动广播，广播接受到消息，发送通知
        Intent i = new Intent(this,NoticeBaordCastReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this,0,i,0);
        //唤醒cpu
   //    long t= System.currentTimeMillis();
        //测试
     //   alarmManager.set(AlarmManager.RTC_WAKEUP,t+5*1000,pi);
        //时间设置，转化成毫秒的格式必须为YYYY-mm-dd hh:mm:ss,不然转化会失败
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int monthday = BaseActivity.getDays(year,month);
        String myMonth="";
        String myDay="";
        if(month<10){
          myMonth = "0"+month;
        }else{
            myMonth=month+"";
        }
        if (day<10){
            myDay="0"+day;
        }else{
            myDay=day+"";
        }
        String  timestr = year+"-"+myMonth+"-"+myDay+" "+"10:"+"00:00";
        //转化为mills
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long millionSeconds;//毫秒
                millionSeconds = sdf.parse(timestr).getTime();
            if (hour>=10){
                //加一天
                if(day+1<10){
                    myDay ="0"+(day+1);
                }else{
                    if (day+1>monthday){
                        //加一月
                        if (month+1<10){
                            myMonth = "0"+(month+1);
                        }else{
                            myMonth = (month+1)+"";
                            if (Integer.valueOf(myMonth) >12){
                                //是否大于12
                                year=year+1;
                                myMonth="01";
                                myDay="01";
                            }else{
                                myDay="01";
                            }
                        }
                    }else{
                        myDay=(day+1)+"";
                    }
                }
                //10点推送消息
                String nstr= year+"-"+myMonth+"-"+myDay+" 10:"+"00:00";
                long str = sdf.parse(nstr).getTime();
                //RTC_WAKEUP是绝对的时间,24小时后重复执行
                if (alarmManager != null) {
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,str,24*60*60*1000,pi);
                }
                Log.e("startTime",str+"");
            }else{
                if (alarmManager != null) {
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,millionSeconds,24*60*60*1000,pi);
                }
                Log.e("startTime",millionSeconds+"");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  START_REDELIVER_INTENT;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //service结束关闭manger
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, NoticeBaordCastReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        if (manager != null) {
            manager.cancel(pi);
        }
    }
}
